package com.rem.rabbitmq.boot.Adelay;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 使用插件实现rabbitMQ 延迟队列
 *
 * @author Rem
 * @date 2022-01-03
 */

@Configuration
public class DelayQueueConfig {

    /**
     * 延迟交换机 队列 routingKey
     */
    public static final String DELAY_EXCHANGE = "DELAY_EXCHANGE";

    public static final String DELAY_ROUTING_KEY = "DELAY_ROUTING_KEY";

    public static final String DELAY_QUEUE = "DELAY_QUEUE";

    @Bean
    public DirectExchange delayExchange() {
        //自定义参数
//        Map<String, Object> arguments = new HashMap<>();
//        //自定义交换机的类型
//        arguments.put("x-delayed-type", "direct");
//        return new CustomExchange(DELAY_EXCHANGE, "x-delayed-message", true, false, arguments);

        //使用api设置
        return ExchangeBuilder.directExchange(DELAY_EXCHANGE).delayed().build();
    }

    @Bean
    public Queue delayQueue() {
        return QueueBuilder.durable(DELAY_QUEUE).build();
    }

    @Bean
    public Binding bindingDelayedQueue(@Qualifier("delayQueue") Queue delayQueue, @Qualifier("delayExchange") Exchange delayExchange) {
        return BindingBuilder.bind(delayQueue).to(delayExchange).with(DELAY_ROUTING_KEY).noargs();
    }

}
